package ${package.Controller};

import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
<#if restControllerStyle>
    import org.springframework.web.bind.annotation.RestController;
<#else>
    import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
    import ${superControllerClassPackage};
</#if>
import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};
import ${package.Entity}.vo.${entity}VO;
import ${package.Entity}.qo.${entity}QO;
import ${package.Entity}.qo.${entity}IdQO;
import ${package.Entity}.qo.${entity}BatchQO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.fuse.common.util.result.ResultJson;
import com.fuse.common.util.result.PageResult;
import org.springframework.http.MediaType;
import java.math.BigDecimal;
import io.aicn.lowcode.engine.util.ControllerUtils;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Arrays;
import java.io.Serializable;

/**
* <p>
    * ${table.comment!} 前端控制器
    * </p>
*
* @author ${author}
* @since ${date}
*/
<#if restControllerStyle>
    @RestController
<#else>
    @Controller
</#if>
@RequestMapping("<#if package.ModuleName?? && package.ModuleName != "">/${package.ModuleName}/<#else>/</#if><#if controllerMappingHyphenStyle>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
@Api(tags = "${apiTagPrefix!}--${table.comment!}管理")
@Validated
<#if kotlin>
    class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
    <#if superControllerClass??>
        public class ${table.controllerName} extends ${superControllerClass} {
    <#else>
        public class ${table.controllerName} {
    </#if>

    @Autowired
    private ${table.serviceName} ${table.entityPath}Service;

<#-- 获取主键字段信息 -->
    <#assign keyField = table.fields[0]>
    <#list table.fields as field>
        <#if field.keyFlag>
            <#assign keyField = field>
            <#break>
        </#if>
    </#list>

    /**
     * 将实体对象列表转换为VO对象列表
     */
    private List<${entity}VO> convertToVOList(List<${entity}> entityList) {
        return ControllerUtils.convertToVOList(entityList, ${entity}VO.class);
    }
    
    /**
     * 将分页实体对象转换为分页VO对象
     */
    private PageResult<${entity}VO> convertToPageResult(IPage<${entity}> entityPage) {
        List<${entity}VO> voList = convertToVOList(entityPage.getRecords());
        return new PageResult<>((int)entityPage.getCurrent(), (int)entityPage.getSize(), (int)entityPage.getTotal(), voList);
    }

    /**
    * 分页查询${table.comment!}列表
    */
    @PostMapping(value = "/page", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "分页查询${table.comment!}列表", notes = "分页查询${table.comment!}列表")
    public ResultJson<PageResult<${entity}VO>> getPage(
    @RequestBody @Valid @ApiParam(value = "分页查询参数", required = true) ${entity}QO qo) {
        try {
            Page<${entity}> page = new Page<>(qo.getPageNum(), qo.getPageSize());
            QueryWrapper<${entity}> queryWrapper = ControllerUtils.createQueryWrapper(qo.getParam());
            IPage<${entity}> result = ${table.entityPath}Service.page(page, queryWrapper);
            return ControllerUtils.handleSuccess(convertToPageResult(result));
        } catch (Exception e) {
            return ControllerUtils.handleException();
        }
    }

    /**
    * 根据ID获取${table.comment!}详情
    */
    @PostMapping(value = "/get", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "根据ID获取${table.comment!}详情", notes = "根据ID获取${table.comment!}详情")
    public ResultJson<${entity}VO> get(
    @RequestBody @Valid @ApiParam(value = "ID查询参数", required = true) ${entity}IdQO qo) {
        try {
            ${entity} entity = ${table.entityPath}Service.getById(qo.getId());
            if (entity == null) {
                return ControllerUtils.handleNotFound();
            }
            ${entity}VO vo = ControllerUtils.convertToVO(entity, ${entity}VO.class);
            return ControllerUtils.handleSuccess(vo);
        } catch (Exception e) {
            return ControllerUtils.handleException();
        }
    }

    /**
    * 新增${table.comment!}
    */
    @PostMapping(value = "/save", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "新增${table.comment!}", notes = "新增${table.comment!}")
    public ResultJson<${entity}VO> save(
    @RequestBody @Valid @ApiParam(value = "新增参数", required = true) ${entity} entity) {
        try {
            boolean result = ${table.entityPath}Service.save(entity);
            if (result) {
                ${entity}VO vo = ControllerUtils.convertToVO(entity, ${entity}VO.class);
                return ControllerUtils.handleSuccess(vo);
            } else {
                return ControllerUtils.handleFailure();
            }
        } catch (Exception e) {
            return ControllerUtils.handleException();
        }
    }

    /**
    * 更新${table.comment!}
    */
    @PostMapping(value = "/update", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "更新${table.comment!}", notes = "更新${table.comment!}")
    public ResultJson<${entity}VO> update(
    @RequestBody @Valid @ApiParam(value = "更新参数", required = true) ${entity} entity) {
        try {
            boolean result = ${table.entityPath}Service.updateById(entity);
            if (result) {
                ${entity}VO vo = ControllerUtils.convertToVO(entity, ${entity}VO.class);
                return ControllerUtils.handleSuccess(vo);
            } else {
                return ControllerUtils.handleFailure();
            }
        } catch (Exception e) {
            return ControllerUtils.handleException();
        }
    }

    /**
    * 删除${table.comment!}
    */
    @PostMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除${table.comment!}", notes = "删除${table.comment!}")
    public ResultJson<Boolean> delete(
    @RequestBody @Valid @ApiParam(value = "删除参数", required = true) ${entity}BatchQO qo) {
        try {
            boolean result = ${table.entityPath}Service.removeByIds(qo.get${keyField.capitalName}s());
            return ControllerUtils.handleSuccess(result);
        } catch (Exception e) {
            return ControllerUtils.handleException();
        }
    }
                                }
</#if>